package com.ys.tdm.dao.code.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.ys.tdm.bean.code.ServiceType;
import com.ys.tdm.dao.code.IServiceTypeDao;
import com.ys.tdm.templ.MybatisTemplate;
import com.ys.tdm.util.StringTools;
import com.ys.tdm.util.UUIDGenerator;




/**
 * 数据库类型信息Dao实现
 * @author hhj
 * @date 2018-03-07 10:23:27
 */
@Repository
public class ServiceTypeDaoImpl extends MybatisTemplate implements IServiceTypeDao {

	@Override
	public ServiceType getServiceTypeById(String id) throws Exception {
		return (ServiceType)this.selectOne("ServiceTypeXML.getServiceTypeById", id);
	}
	
	@Override
	public List<ServiceType> getServiceTypeByIds(String ids) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		return this.selectList("ServiceTypeXML.getServiceTypeByIds", params);
	}
	
	@Override
	public List<ServiceType> getServiceTypeByIdsList(List<String> ids) throws Exception {
		return this.selectList("ServiceTypeXML.getServiceTypeByIdsList", ids);
	}

	@Override
	public List<ServiceType> getAll(ServiceType serviceType) throws Exception {
		return this.selectList("ServiceTypeXML.getAll", serviceType);
	}

	/*@Override
	public PagerModel<ServiceType> getPagerModelByQuery(ServiceType serviceType, Query query)
			throws Exception {
		return this.getPagerModelByQuery(serviceType, query, "ServiceTypeXML.getPagerModelByQuery");
	}*/
	
	@Override
	public int getByPageCount(ServiceType serviceType)throws Exception {
		return this.selectOne("ServiceTypeXML.getByPageCount", serviceType);
	}

	@Override
	public void insertServiceType(ServiceType serviceType) throws Exception {
		if (null != serviceType) {
			serviceType.setId(UUIDGenerator.generate());
			serviceType.setCreateTime(new Date());
			serviceType.setUpdateTime(new Date());
			this.insert("ServiceTypeXML.insertServiceType", serviceType);
		}
	}
	
	@Override
	public void insertServiceTypeBatch(List<ServiceType> serviceTypes) throws Exception {
		if(CollectionUtils.isNotEmpty(serviceTypes)){
			for (ServiceType serviceType : serviceTypes) {
				if (null != serviceType) {
					serviceType.setId(UUIDGenerator.generate());
					serviceType.setCreateTime(new Date());
				}
			}
			this.insert("ServiceTypeXML.insertServiceTypeBatch", serviceTypes);
		}
	}
	
	@Override
	public void delServiceTypeById(String id) throws Exception {
		this.delete("ServiceTypeXML.delServiceTypeById", id);
	}
	
	@Override
	public void delServiceTypeByIds(String ids) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		this.delete("ServiceTypeXML.delServiceTypeByIds", params);
	}
	
	@Override
	public void delServiceTypeByIdsList(List<String> ids) throws Exception {
		this.delete("ServiceTypeXML.delServiceTypeByIdsList", ids);
	}
	
	@Override
	public int updateServiceType(ServiceType serviceType) throws Exception {
		if (null != serviceType) {
			serviceType.setUpdateTime(new Date());
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(serviceType);
			return this.update("ServiceTypeXML.updateServiceType", serviceType);
		} else {
			return 0;
		}
	}
	
	@Override
	public int updateServiceTypeByIds(String ids,ServiceType serviceType) throws Exception {
		ids = StringTools.converString(ids);
		if (StringUtils.isNotBlank(ids) && null != serviceType) {
			serviceType.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("ids", ids);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(serviceType);
			params.put("serviceType", serviceType);
			return this.update("ServiceTypeXML.updateServiceTypeByIds", params);
		} else {
			return 0;
		}
		
	}
	
	@Override
	public int updateServiceTypeByIdsList(List<String> ids,ServiceType serviceType) throws Exception {
		if (CollectionUtils.isNotEmpty(ids) && null != serviceType) {
			serviceType.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("ids", ids);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(serviceType);
			params.put("serviceType", serviceType);
			return this.update("ServiceTypeXML.updateServiceTypeByIdsList", params);
		} else {
			return 0;
		}
	}
	
	@Override
	public int updateServiceTypeList(List<ServiceType> serviceTypes) throws Exception {
		if(CollectionUtils.isNotEmpty(serviceTypes)){
			for (ServiceType serviceType : serviceTypes) {
				if (null != serviceType) {
					serviceType.setUpdateTime(new Date());
					/**清理不需要更新的数据*/
					this.cleanWhenUpdate(serviceType);
				}
			}
			return this.update("ServiceTypeXML.updateServiceTypeList", serviceTypes);
		} else {
			return 0;
		}
	}
	
	/**
	 * 清理不需要更新的数据
	 * @param orderRebok
	 * @Description:
	 * @author wentaoxiang 2016年6月1日 下午5:19:16
	 */
	private void cleanWhenUpdate(ServiceType serviceType) {
		serviceType.setCreateTime(null);
		serviceType.setCreator(null);
	}
	
	//------------api------------
}

